package cn.wolfcode.shop.mapper;

import cn.wolfcode.shop.domain.Good;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.List;

@Mapper
public interface GoodMapper {
    /**
     * @param ids
     * @return
     * @SelectProvider标签 type表示要使用哪个类的 method方法  来生成select的sql字符串
     */
    @SelectProvider(type = GoodSelectProvider.class, method = "queryByIds")
    List<Good> querybyIds(@Param("ids") List ids);

    class GoodSelectProvider {
        /**
         * 通过ids 生成sql
         * @param ids
         * @return
         */
        public String queryByIds(@Param("ids") List ids) {
            StringBuilder sql = new StringBuilder(30);
            sql.append("select * from t_goods ");
            if (ids != null && ids.size() > 0) {
                sql.append("where id in (");
                for (int i = 0; i < ids.size(); i++) {
                    if (i > 0) {
                        sql.append(",");
                    }
                    sql.append(ids.get(i));
                }
                sql.append(")");
            }
            return sql.toString();
        }
    }
}
